<html>
<head><meta charset="utf-8"><title>why can&#x27;t you use a generic type in a local static? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20can&#x27;t.20you.20use.20a.20generic.20type.20in.20a.20local.20static.3F.html">why can&#x27;t you use a generic type in a local static?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="134781323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20can%27t%20you%20use%20a%20generic%20type%20in%20a%20local%20static%3F/near/134781323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20can&#x27;t.20you.20use.20a.20generic.20type.20in.20a.20local.20static.3F.html#134781323">(Sep 27 2018 at 23:48)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">example</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">EXAMPLE</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">None</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<div class="codehilite"><pre><span></span>error[E0401]: can&#39;t use type parameters from outer function
 --&gt; src/lib.rs:2:28
  |
1 | fn example&lt;T&gt;() {
  |    ------- - type variable from outer function
  |    |
  |    try adding a local type parameter in this method instead
2 |     static EXAMPLE: Option&lt;T&gt; = None;
  |                            ^ use of type variable from outer function
</pre></div>


<p>Ignoring the poor suggestion in the error message, what's the real reason preventing this?</p>



<a name="134781349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20can%27t%20you%20use%20a%20generic%20type%20in%20a%20local%20static%3F/near/134781349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20can&#x27;t.20you.20use.20a.20generic.20type.20in.20a.20local.20static.3F.html#134781349">(Sep 27 2018 at 23:49)</a>:</h4>
<p>Is it that the compiler doesn't want to (can't?) create N instances of <code>EXAMPLE</code>, one for each concrete instance of <code>T</code> that's used at final compile time?</p>



<a name="134810661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20can%27t%20you%20use%20a%20generic%20type%20in%20a%20local%20static%3F/near/134810661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20can&#x27;t.20you.20use.20a.20generic.20type.20in.20a.20local.20static.3F.html#134810661">(Sep 28 2018 at 08:56)</a>:</h4>
<blockquote>
<p>Is it that the compiler doesn't want to (can't?) create N instances of <code>EXAMPLE</code>, one for each concrete instance of <code>T</code> that's used at final compile time?</p>
</blockquote>
<p>My suspicion is that the concrete reason what you describe is unsupported is just the more general "we don't let you inherit generics in nested items." I.e. there's no <em>reason</em> per se that we couldn't allow nesting of <code>fn foo&lt;T&gt;(...) { fn bar(x: T) { ... } ... }</code>. But we don't , and <em>that</em> is more of an opinionated design decision...</p>



<a name="134810690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20can%27t%20you%20use%20a%20generic%20type%20in%20a%20local%20static%3F/near/134810690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20can&#x27;t.20you.20use.20a.20generic.20type.20in.20a.20local.20static.3F.html#134810690">(Sep 28 2018 at 08:57)</a>:</h4>
<p>having said that, the hypothesis you put forward is a pretty good description of why the lang team might <em>balk</em> at adding support for allowing local statics in a generic function...</p>



<a name="134810758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20can%27t%20you%20use%20a%20generic%20type%20in%20a%20local%20static%3F/near/134810758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20can&#x27;t.20you.20use.20a.20generic.20type.20in.20a.20local.20static.3F.html#134810758">(Sep 28 2018 at 08:58)</a>:</h4>
<p>(i.e., I'd summarize it as "people expect there to be a single instance of a given static. A generic static inherently breaks that.")</p>



<a name="134810790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/why%20can%27t%20you%20use%20a%20generic%20type%20in%20a%20local%20static%3F/near/134810790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/why.20can&#x27;t.20you.20use.20a.20generic.20type.20in.20a.20local.20static.3F.html#134810790">(Sep 28 2018 at 08:59)</a>:</h4>
<p>also, I don't know if the language itself <em>guarantees</em> that it will always use a monomorphization strategy for implementing all generics...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>